Solr是托管于Apache基金会的Lucene项目的一个子工程,它是一个使用Lucene内核的通用搜索服务器, 支持基于HTTP的XML、JSON访问,并且提供Python和Ruby API,具备WEB管理界面,有高亮支持,具备Cache、集群能力。
使用Solr构建搜索,简单好用,代码量也非常小。
部署Solr环境
准备必要的依赖环境
apt-get update
apt-get -y install wget
apt-get -y install lsof
apt-get -y install openjdk-7-jdk
下载和运行Solr
wget http://apache.fayea.com/lucene/solr/5.3.1/solr-5.3.1.tgz
tar -zxvf solr-5.3.1.tgz -C /usr/local/
cd /usr/local && ln -s solr-5.3.1 solr
启动solr
bin/solr start
完成后Solr将侦听在8983端口,可以使用浏览器打开http://localhost:8983进行查看和管理
使Solr支持中文分词,可以使用自带的分词组件,默认没有加载,需要人工将组件拷贝到运行目录,完成后需要重新运行Solr
cp /usr/local/solr/contrib/analysis-extras/lucene-libs/lucene-analyzers-smartcn-5.3.1.jar /usr/local/solr/server/solr-webapp/webapp/WEB-INF/lib/
停止Solr
bin/solr stop -p 8983
初始化Solr配置
Solr以Collection为单位组织搜索。
1、建立Collection,完成Collection的建立后,将开始在Collection里面保存搜索数据。
cd /usr/local/solr && bin/solr create -c wiki.xglabc.com
2、为Collection配置字段,也就是你要保存的记录里面的字段的类型等。 可以使用Schema API进行操作,或者直接修改配置文件。
1)使用Schema API
添加字段类型
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type" : {
"name":"text_cn_smart",
"class":"solr.TextField",
"positionIncrementGap":"100",
"analyzer" : {
"tokenizer":{"class":"solr.SmartChineseSentenceTokenizerFactory"},
"filters":[{"class":"solr.SmartChineseWordTokenFilterFactory"}]
}
}
}' http://localhost:8983/solr/wiki.xglabc.com/schema
添加字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field":{
"name":"rev_id",
"type":"tlongs",
"multiValued":false,
"required":true},
"add-field":{
"name":"title",
"type":"text_cn_smart",
"indexed":true,
"stored":true,
"multiValued":false,
"required":true},
"add-field":{
"name":"content",
"type":"text_cn_smart",
"indexed":true,
"stored":true,
"multiValued":false,
"required":true},
"add-field":{
"name":"last_modify",
"type":"tdates",
"indexed":true,
"stored":true,
"multiValued":false,
"required":true}
}' http://localhost:8983/solr/wiki.xglabc.com/schema
修改字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"replace-field":{
"name":"id",
"type":"string",
"indexed":true,
"stored":true,
"multiValued":false,
"required":true},
}' http://localhost:8983/solr/wiki.xglabc.com/schema
2)使用配置文件方式
vi /usr/local/solr/server/solr/wiki.xglabc.com/conf/managed-schema
<fieldType name="text_cn_smart" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.SmartChineseSentenceTokenizerFactory"/>
<filter class="solr.SmartChineseWordTokenFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.SmartChineseSentenceTokenizerFactory"/>
<filter class="solr.SmartChineseWordTokenFilterFactory"/>
</analyzer>
</fieldType>
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="rev_id" type="tlongs" multiValued="false" required="true"/>
<field name="title" type="text_cn_smart" multiValued="false" indexed="true" stored="true"/>
<field name="content" type="text_cn_smart" multiValued="false" indexed="true" stored="true"/>
修改文件方式完成后需要重启才可生效
bin/solr stop -p 8983
bin/solr start
使用Solr
如果你使用Python作为开发语言可以选择: - solrcloudpy
用法参考下面 - https://pypi.python.org/pypi/solrcloudpy - http://dfdeshom.github.io/solrcloudpy/
